home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / motif / obuffer.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  10KB  |  362 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * obuffer : openGL (motif) demo to switch between single/double buffer mode
  25.  *
  26.  * Author : Yusuf Attarwala
  27.  *          SGI - Applications
  28.  * Date   : Jan 93
  29.  *
  30.  *    note : the main intent of this program is to demo the buffer
  31.  *           switching functionality, hence the rendering is kept
  32.  *           simple (wireframe).
  33.  * 
  34.  *    press  left   button for single buffer
  35.  *           middle button for double buffer
  36.  *           right  button for animation
  37.  *
  38.  *---------------------------------------------------------------------------*/
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41.  
  42. #include <Xm/Xm.h> 
  43. #include <Xm/Frame.h>               /* for frame widgets */
  44. #include <X11/keysym.h>             /* keyboard translations */
  45. #include <X11/StringDefs.h>
  46.  
  47. #include <GL/gl.h>                  /* gl includes */
  48. #include <GL/glu.h>                 /* utility library includes */
  49. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  50.  
  51. /* function declarations */
  52.  
  53. void 
  54.     createToplevel(void),
  55.     createSingleBuffer(void),
  56.     createDoubleBuffer(void),
  57.     toSingleBuffer(void),
  58.     toDoubleBuffer(void),
  59.     animation(void),
  60.     drawScene(void),
  61.     setMatrix(void),
  62.     exposeCB(Widget,XtPointer,XtPointer),
  63.     resizeCB(Widget,XtPointer,XtPointer),
  64.     initCB(Widget,XtPointer,XtPointer),
  65.     inputCB(Widget,XtPointer,XtPointer);
  66.  
  67. /* global variables */
  68.             
  69. Display      *display;             /* current display */
  70. XtAppContext appContext;           /* X application context */
  71. int           doubleBuffer;        /* flag for current buffer status */
  72. float         ax,ay,az;            /* angles for animation */
  73. GLUquadricObj *quadObj;            /* used in drawscene */
  74.  
  75. Widget       toplevel,       /* toplevel shell */
  76.              frameSB,        /* frame to hold single buffer gl widget */
  77.              frameDB,        /* frame to hold double buffer gl widget */
  78.              glwSB,
  79.              glwDB,
  80.              glw;            /* current widget */
  81.              
  82. GLXContext   glxc;           /* current glx context */
  83.  
  84. Arg args[20];
  85. int acnt;
  86.  
  87. void 
  88. main(int argc, char** argv)
  89. {
  90.     XtToolkitInitialize();
  91.     appContext = XtCreateApplicationContext();
  92.     display    = XtOpenDisplay(appContext, NULL, "Obuffer","obuffer",NULL,0,
  93.                               &argc,argv);
  94.     if (!display) {
  95.         printf("%s : Unable to open display\n",argv[0]);
  96.         exit(0);
  97.     }
  98.  
  99.     printf("\n---------------------------------------------\n");
  100.     printf("Demo to toggle single/double buffer mode \n\n");
  101.     printf("Press:  left   button for single buffer\n\
  102.         middle button for double buffer\n\
  103.         right  button for animation\n");
  104.  
  105.  
  106.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  107.     createToplevel();             /* create widget hierarchy */
  108.     XtAppMainLoop(appContext);
  109. }
  110.  
  111.  
  112.  
  113. void
  114. createToplevel()
  115. {
  116.     acnt = 0;
  117.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  118.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  119.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  120.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  121.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  122.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  123.     toplevel  = XtAppCreateShell("toplevel","obuffer",
  124.                                   applicationShellWidgetClass,
  125.                                   display,args,acnt);
  126.  
  127.     /* create two drawing area widgets as children of
  128.        toplevel, only manage one at a time */
  129.  
  130.     createDoubleBuffer();
  131.     createSingleBuffer();
  132.  
  133.     /* realize widget hierarchy */
  134.     XtRealizeWidget(toplevel);
  135.  
  136.     toDoubleBuffer();  /* let the default be double buffer */
  137. }
  138.  
  139. void
  140. createSingleBuffer()
  141. {
  142.     acnt = 0;
  143.     frameSB = XtCreateWidget("frameSB", xmFrameWidgetClass,
  144.                               toplevel, args, acnt);
  145.  
  146.     /* NOTE : do not manage child here */
  147.  
  148.     acnt = 0;
  149.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  150.     XtSetArg(args[acnt], GLwNdoublebuffer,       FALSE); acnt++;
  151.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  152.     glwSB = GLwCreateMDrawingArea(frameSB, "glwSB", args, acnt);
  153.  
  154.     XtManageChild(glwSB);
  155.  
  156.     /* register callbacks */
  157.     XtAddCallback(glwSB, GLwNexposeCallback, exposeCB, NULL);
  158.     XtAddCallback(glwSB, GLwNresizeCallback, resizeCB, NULL);
  159.     XtAddCallback(glwSB, GLwNginitCallback,  initCB,   NULL);
  160.     XtAddCallback(glwSB, GLwNinputCallback,  inputCB,  NULL);
  161.  
  162. }
  163.  
  164. void
  165. createDoubleBuffer()
  166. {
  167.     acnt = 0;
  168.     frameDB = XtCreateWidget("frameDB", xmFrameWidgetClass,
  169.                               toplevel, args, acnt);
  170.  
  171.     XtManageChild(frameDB);
  172.  
  173.     acnt = 0;
  174.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  175.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;  /* NOTE */
  176.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  177.     glwDB = GLwCreateMDrawingArea(frameDB, "glwDB", args, acnt);
  178.  
  179.     XtManageChild(glwDB);
  180.  
  181.     /* register callbacks */
  182.     XtAddCallback(glwDB, GLwNexposeCallback, exposeCB, NULL);
  183.     XtAddCallback(glwDB, GLwNresizeCallback, resizeCB, NULL);
  184.     XtAddCallback(glwDB, GLwNginitCallback,  initCB,   NULL);
  185.     XtAddCallback(glwDB, GLwNinputCallback,  inputCB,  NULL);
  186.  
  187. }
  188.  
  189. void
  190. toSingleBuffer(void)
  191. {
  192.     acnt = 0;
  193.     XtSetArg(args[acnt],XmNtitle, "SINGLE buffer");acnt++;
  194.     XtSetValues(toplevel,args,acnt);
  195.  
  196.     XtManageChild(frameSB);
  197.     XtUnmanageChild(frameDB);
  198.     glw = glwSB;
  199.     doubleBuffer = FALSE;
  200.  
  201.     XtSetArg(args[0], XmNuserData, &glxc);
  202.     XtGetValues(glw, args, 1);
  203.     GLwDrawingAreaMakeCurrent(glw, glxc);
  204.     setMatrix();
  205.     drawScene();
  206. }
  207.  
  208. void
  209. toDoubleBuffer(void)
  210. {
  211.     acnt = 0;
  212.     XtSetArg(args[acnt],XmNtitle, "DOUBLE buffer");acnt++;
  213.     XtSetValues(toplevel,args,acnt);
  214.  
  215.     XtManageChild(frameDB);
  216.     XtUnmanageChild(frameSB);
  217.     glw = glwDB;
  218.     doubleBuffer = TRUE;
  219.  
  220.     XtSetArg(args[0], XmNuserData, &glxc);
  221.     XtGetValues(glw, args, 1);
  222.     GLwDrawingAreaMakeCurrent(glw, glxc);
  223.     setMatrix();
  224.     drawScene();
  225.  
  226. }
  227.  
  228. void
  229. drawScene(void)
  230. {
  231.     glClearColor(0.0, 0.0, 0.0, 0.0);
  232.     glClear(GL_COLOR_BUFFER_BIT);
  233.  
  234.     glPushMatrix();
  235.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  236.     glColor3f (0.7, 0.3, 1.0);
  237.     glTranslatef (12.0,12.0,0.0);
  238.     glRotatef(ax,1.0,0.0,0.0);
  239.     glRotatef(-ay,0.0, 1.0, 0.0);
  240.     gluCylinder (quadObj, 2.0,5.0,10.0,16,6);
  241.     glPopMatrix();
  242.  
  243.     glPushMatrix();
  244.     gluQuadricDrawStyle (quadObj, GLU_FILL);
  245.     glColor3f (3.2, 0.8, 1.0);
  246.     glTranslatef (15.0,15.0,0.0);
  247.     glRotatef(ax,1.0,0.0,0.0);
  248.     glRotatef(ay,0.0, 1.0, 0.0);
  249.     glRotatef(az,0.0,0.0,1.0);
  250.     gluDisk(quadObj,2.0,5.0,16,4);
  251.  
  252.     gluQuadricDrawStyle (quadObj, GLU_SILHOUETTE);
  253.     glColor3f (1.0, 0.2, 1.0);
  254.     glTranslatef (7.0,7.0,0.0);
  255.     glRotatef(-ax,1.0,0.0,0.0);
  256.     glRotatef(az,0.0,0.0,1.0);
  257.     gluPartialDisk (quadObj, 0.0,5.0,20,3,135.0,270.0);
  258.  
  259.     glPopMatrix();
  260.     glFlush();
  261.  
  262.     if (doubleBuffer) glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  263. }
  264.  
  265. void
  266. setMatrix(void)
  267. {
  268.     glMatrixMode(GL_PROJECTION);
  269.     glLoadIdentity();
  270.     glOrtho(0.0,32.0,0.0,32.0,-15.0,15.0);
  271.     glMatrixMode(GL_MODELVIEW);
  272.     glLoadIdentity();
  273. }
  274.  
  275. void
  276. animation(void)
  277. {
  278.     register int i;
  279.  
  280.     for (i=0;i<70;i++) {
  281.         ax += 5.0;
  282.         ay -= 2.0;
  283.         az += 5.0;
  284.         if (ax >= 3600)  ax = 0.0;
  285.         if (ay <= -3600) ay = 0.0;
  286.         if (az >= 3600)  az = 0.0;
  287.         drawScene();
  288.     }
  289. }
  290.  
  291. void 
  292. inputCB(Widget w, XtPointer client_data, XtPointer call)
  293. {
  294.     char buffer[1];
  295.     KeySym keysym;
  296.     GLwDrawingAreaCallbackStruct *call_data;
  297.  
  298.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  299.  
  300.     switch(call_data->event->type) {
  301.     case ButtonPress:
  302.         switch (call_data->event->xbutton.button) {
  303.         case Button1:
  304.             toSingleBuffer();
  305.             break;
  306.         case Button2 :
  307.             toDoubleBuffer();
  308.             break;
  309.         case Button3 :
  310.             animation();
  311.             break;
  312.         }
  313.         break;
  314.     default:
  315.         break;
  316.     }
  317. }
  318.  
  319. void 
  320. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  321. {
  322.     GLwDrawingAreaCallbackStruct *call_data;
  323.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  324.  
  325.     XtSetArg(args[0], XmNuserData, &glxc);
  326.     XtGetValues(w, args, 1);
  327.  
  328.     GLwDrawingAreaMakeCurrent(w, glxc);
  329.     glViewport(0, 0, call_data->width, call_data->height);
  330.     drawScene();
  331. }
  332.  
  333. void 
  334. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  335. {
  336.     GLwDrawingAreaCallbackStruct *call_data;
  337.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  338.  
  339.     XtSetArg(args[0], XmNuserData, &glxc);
  340.     XtGetValues(w, args, 1);
  341.  
  342.     GLwDrawingAreaMakeCurrent(w, glxc);
  343.     drawScene();
  344. }
  345.  
  346. void
  347. initCB(Widget w, XtPointer client_data, XtPointer call)
  348. {
  349.     XVisualInfo *vi;
  350.  
  351.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  352.     XtGetValues(w, args, 1);
  353.  
  354.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  355.     XtSetArg(args[0], XmNuserData, glxc);
  356.     XtSetValues(w, args, 1);
  357.  
  358. }
  359.  
  360.  
  361.  
  362.